<?php

include '../../!inc.php';

require($StudyBuddyClasses.'Base.php');
require($StudyBuddyClasses.'Api'.DIRECTORY_SEPARATOR.'Api.php');

try{

	$oRequest = $oRegistry->Request;
	$a  = $oRequest['a'];
	$v  = $oRequest->get('v', 'i', 1);

	d('a: '.$a.' $oRequest: '.print_r($oRequest->getArray(), 1));
	$controller = ucfirst($a);
	include($StudyBuddyClasses.'Api'.DIRECTORY_SEPARATOR.'v'.$v.DIRECTORY_SEPARATOR.$controller.'.php');
	$class = '\StudyBuddy\\Api\\v'.$v.'\\'.$controller;
	d('class: '.$class);

	$o = new $class($oRegistry);
	$Response = $o->getResponse();
	$Response->send();
	fastcgi_finish_request();

} catch (\Exception $e){

	header("HTTP/1.0 500 Exception");
	header("Content-Type:text/html; charset=utf-8");
	$err = strip_tags($e->getMessage());
	echo $err;
	fastcgi_finish_request();

	$extra = (isset($_SERVER)) ? ' $_SERVER: '.print_r($_SERVER, 1) : ' no $_SERVER';
	$extra .= ' file: '.$e->getFile(). ' line: '.$e->getLine().' trace: '.$e->getTraceAsString();

	if(strlen(trim(constant('STUDYBUDDY_DEVELOPER_EMAIL'))) > 1){
		@mail(STUDYBUDDY_DEVELOPER_EMAIL, '500 Error in index.php', $err.$extra);
	}
}


